package 
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import ginger.geom.RectangleShape;
	import ginger.ui.simple.SimpleLayout;
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.FilterShortcuts;
	import ginger.ui.simple.SimpleTwoDisplayObjButton;
	import ginger.utils.NetUtil;
	import hwsoft.webservice_2_0.utils.ParseUtil;
	import UILib.LeftOnBtnBmpdata;
	import UILib.LeftOutBtnBmpdata;
	import UILib.RightOnBtnBmpdata;
	import UILib.RightOutBtnBmpdata;

	/**
	 * ...
	 * @author Ning
	 */
	public class Wall extends Sprite 
	{
		private var actionObjects:Array;
		private var _radius:Number =450;

		private var _objctNum:uint = 10;
		private var _actionObject:ActionObject;
		private var _holder:Sprite;
		private var _hoaderMask:RectangleShape;
		private var _hoaderBox:Sprite;
		
		private var _leftBtn:SimpleButton;
		private var _rightBtn:SimpleButton;
		
		private var _mouseHolder:Boolean;
		
		//摩擦力
		private var _friction:Number = 0.4; 
		//缓动系数
		private var _easing:Number = 0.1;
		//目标角度记录
		private var _oldTargetRotation:Number;
		//目标角度
		private var _targetRotation:Number = 0;
		private function set targetRotation(value:Number):void
		{
			_oldTargetRotation = value;
			_targetRotation = value;
		}
		/**
		 * 设置目标角度百分比
		 */
		public function set targetRotationPer(value:Number):void
		{
			this.targetRotation = 360*value;
		}
		//速度
		private var _v:Number = 2;
		
		

		public function Wall():void 
		{
			if (stage) onAddedToStage();
			else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			// entry point
			setTimeout(init, 200);
		}
		private function init():void
		{	
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			Global.STAGE = this.stage;
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			
			var obj:Object = NetUtil.getFlashVarsParam(this.root, 'config');
			var url:String = obj == null?'asset/data.xml':String(obj);
			
			loader.load(new URLRequest(url));
		}
		private function onComplete(evt:Event):void
		{
			_leftBtn = new SimpleButton(new Bitmap(new LeftOutBtnBmpdata),new Bitmap(new LeftOnBtnBmpdata),new Bitmap(new LeftOnBtnBmpdata),new Bitmap(new LeftOnBtnBmpdata));
			_rightBtn = new SimpleButton(new Bitmap(new RightOutBtnBmpdata), new Bitmap(new RightOnBtnBmpdata), new Bitmap(new RightOnBtnBmpdata), new Bitmap(new RightOnBtnBmpdata));
			
			
			var data:Array = ParseUtil.parse(XML(evt.target.data), PhotoVO);
			
			_hoaderBox = new Sprite();
			
			_holder = new Sprite();
			
			_hoaderBox.addChild(_holder);
			addChild(_hoaderBox);
			
			actionObjects = [];
			
			for (var i:int = 0; i < _objctNum; i++ )
			{
				var angle:Number = Math.PI*2 / _objctNum * i;
				var data1:PhotoVO = data[i];
				var actionObject:ActionObject = new ActionObject(data1);
				actionObject.addEventListener(ActionObject.OVER, onOver);
				actionObject.addEventListener(ActionObject.OUT, onOut);
				actionObject.x = Math.cos(angle) * _radius;
				actionObject.z = Math.sin(angle) * _radius;
				actionObject.rotationY = -360 / _objctNum * i+90;
				_holder.addChild(actionObject);
				actionObjects.push(actionObject);
			}
			
			Tweener.addTween(this, { time:1.5, alpha:1 } );
			
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			_holder.z =350;
	//		stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(Event.RESIZE, onResize);
			onResize();
			this.alpha = 0;
			_holder.addEventListener(MouseEvent.ROLL_OVER, onHolerOver);
			_holder.addEventListener(MouseEvent.ROLL_OUT, onHolerOut);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
			
			addChild(_leftBtn);
			addChild(_rightBtn)
			
			_leftBtn.addEventListener(MouseEvent.CLICK, onLeftClick);
			_rightBtn.addEventListener(MouseEvent.CLICK, onRightClick);
			
			
			this.targetRotationPer = 1;
			_leftBtn.alpha = _rightBtn.alpha = 0;
			_leftBtn.x = stage.stageWidth / 2 -100;
			_rightBtn.x = stage.stageWidth / 2 +100;
			Tweener.addTween(_leftBtn, { time:1, alpha:1, x:stage.stageWidth / 2 - 300 } );
			Tweener.addTween(_rightBtn, { time:1, alpha:1, x:stage.stageWidth / 2 + 300 } );
		}
		private function sortItems():void
		{
			actionObjects.sort(dethSort);
			for (var i:int = 0; i < actionObjects.length; i++ )
			{
				_holder.addChildAt(actionObjects[i] as DisplayObject, i);
				
			}
		}
		private function dethSort(objA:DisplayObject, objB:DisplayObject):int
		{
			var posA:Vector3D = objA.transform.matrix3D.position;
			posA = _holder.transform.matrix3D.deltaTransformVector(posA);
			var posB:Vector3D = objB.transform.matrix3D.position;
			posB = _holder.transform.matrix3D.deltaTransformVector(posB);
			return posB.z - posA.z;
		}
		private function onOver(evt:Event):void
		{
			var target:ActionObject = evt.target as ActionObject;
			Tweener.addTween(_holder, { time:1.5, z: -200 } );
			target.parent.swapChildrenAt(target.parent.getChildIndex(target), target.parent.numChildren - 1);
		}
		private function onOut(evt:Event):void
		{
			var target:ActionObject = evt.target as ActionObject;
			Tweener.addTween(_holder, { time:1.5, z: -400 } );
		}
		private function onEnterFrame(evt:Event):void
		{
			if (stage.stageWidth / 2 -stage.mouseX > 0)
			   _targetRotation = _oldTargetRotation +5;
			else 
			   _targetRotation = _oldTargetRotation -10;
			    
		//	var pre:Number = (stage.stageWidth / 2 -stage.mouseX)/(stage.stageWidth / 2)*.1;
		//	this.targetRotationPer = pre;
			
			if (!isNaN(_targetRotation)) {
				var dy:Number = _targetRotation - _holder.rotationY;
				//距离和速度成反比
				_v = dy * _easing;
			}
			else {
				//使用磨擦力(速度*磨擦力)
			    _v *= _friction;
			}
			
			_holder.rotationY += _v;
			sortItems();
			for (var i:int = 0; i < actionObjects.length; i++ )
			{
				var actionObject:ActionObject = actionObjects[i] as ActionObject;
				var pos3D:Vector3D = actionObject.transform.matrix3D.position;
			    pos3D = _holder.transform.matrix3D.deltaTransformVector(pos3D);
				FilterShortcuts.init();
				var blur:Number = (pos3D.z+500) / 1000 * 10;
				if (blur > 10)
					blur = 10;
				else if(blur<0)
					blur = 0;
					
				var alpha:Number = 1 - (pos3D.z + 500) / 1000*.5;
				
				
				Tweener.addTween(actionObjects[i],{time:.5, alpha:alpha, _Blur_blurX:blur,_Blur_blurY:blur});

			}
		}
		private function onLeftClick(evt:MouseEvent):void
		{
			this.targetRotation = _oldTargetRotation-36;
		}
		private function onRightClick(evt:MouseEvent):void
		{
			this.targetRotation = _oldTargetRotation+36;
		}
		
		private function onKeyDown(evt:KeyboardEvent):void
		{

		}
		private function onWheel(evt:MouseEvent):void
		{

		}
		private function onResize(evt:Event=null):void
		{
			_hoaderBox.x = stage.stageWidth / 2;
			_hoaderBox.y = stage.stageHeight / 2;
			_leftBtn.y = _rightBtn.y = _hoaderBox.y-150 ;
			_leftBtn.x = _hoaderBox.x - 300;
			_rightBtn.x = _hoaderBox.x + 300;
		}
		private function onHolerOver(evt:MouseEvent):void
		{
			_mouseHolder = true;
		}
		private function onHolerOut(evt:MouseEvent):void
		{
			_mouseHolder = false;
		}
	}
}